package ie.lawlor.amvc.patterns.standard;

import ie.lawlor.amvc.AbstractModel;
import ie.lawlor.amvc.event.Event;
import ie.lawlor.amvc.event.ExceptionPayload;
import ie.lawlor.amvc.event.ReflectionEventHandler;
import ie.lawlor.amvc.event.ThreadedReflectionEventHandler;



/**
 * @author blawlor
 *
 * All pattern models will be based on this class. The standard model implements
 * Reply, Notification, Client-side persistence, visibilty-related resynchronization,
 * Exception and Message handling.
 */
public class StandardModel extends AbstractModel {
  
  /**
   * @param threadingEnabled
   */
  public StandardModel(String name, boolean threadingEnabled) {
    super(name, threadingEnabled?new ThreadedReflectionEventHandler():new ReflectionEventHandler());
  }
  
  public StandardModel(String name) {
    this(name, false);
  }
  
  public void reply(Event source, Event reply){
    source.getSourceController().eventIn(reply);
  }
  
  protected void fireUpdateView(){
    fire(getController().getEvent(StandardController.UPDATEVIEW), this);
  }
  
  protected void fireShowView(){
    fire(getController().getEvent(StandardController.SHOWVIEW), this);
  }
  
  protected void fireHideView(){
    fire(getController().getEvent(StandardController.HIDEVIEW), this);
  }
  
  protected void fireException(Throwable t, String message){
    fire(getController().getEvent(StandardController.EXCEPTION), new ExceptionPayload(t, message));
  }
  
  protected void fireException(Throwable t){
    fireException(t, t.getMessage());
  }
  
  protected void fireMessage(String message){
    fire(getController().getEvent(StandardController.MESSAGE), message);
  }
  
  public void fireCursorWaiting(){
    fire(getController().getEvent(StandardController.CURSOR_WAITING), this);
  }
  
  public void fireCursorNormal(){
    fire(getController().getEvent(StandardController.CURSOR_NORMAL), this);
  }
  
  
}
